//
//  LYCycleView.m
//  CycleScrollViewDemo
//
//  Created by lanou3g on 16/4/19.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "LYCycleView.h"

@interface LYCycleView ()<UIScrollViewDelegate>
/// scrollView
@property (nonatomic,strong) UIScrollView *imgScrollView;
/// pageControl
@property (nonatomic,strong) UIPageControl *imgPageControl;
/// ImageViewArray
@property (nonatomic,strong) NSMutableArray *imageViewArray;
/// 存放图片的数组
@property (nonatomic,strong) NSMutableArray *imageArray;
/// 当前展示的图片下标
@property (nonatomic,assign) NSInteger index;
/// 定时器
@property (nonatomic,strong) NSTimer *imgTimer;
/// 点击回调block
@property (nonatomic,copy) void(^tapBlock)(NSInteger index);
/// 时间间隔
@property (nonatomic,assign) NSTimeInterval interval;
@end


@implementation LYCycleView

/// 初始化
- (instancetype)initWithFrame:(CGRect)frame InterVal:(NSTimeInterval)interval {
    self = [super initWithFrame:frame];
    if (self) {
        self.imageViewArray = [NSMutableArray new];
        self.imageArray = [NSMutableArray new];
        
        // 间隔时间
        self.interval = interval;
        
        // 创建UIScrollView
        self.imgScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        [self addSubview:self.imgScrollView];
        // 属性
        self.imgScrollView.contentSize = CGSizeMake(frame.size.width * 3, frame.size.height);
        self.imgScrollView.showsHorizontalScrollIndicator = NO;
        self.imgScrollView.pagingEnabled = YES;
        // 设置代理
        self.imgScrollView.delegate = self;
        
        
        
        for (int i = 0; i < 3; i++) {
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height)];
            
            /// 添加到父视图、imageViewArray数组
            [self.imgScrollView addSubview:imgView];
            [self.imageViewArray addObject:imgView];
        }
        
    }
    return self;
}

/// 图片滚动的方法
- (void)timerAction {
    [self.imgScrollView setContentOffset:CGPointMake(self.imgScrollView.frame.size.width * 2, 0) animated:YES];
    // 改变index
    self.index = (self.index + 1) % self.imageNameArray.count;
}
#pragma mark - 滚动动画结束
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    /**
     * 回位
     */
    [self.imgScrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:NO];
    [self layoutImages];
}

/// 图片名数组setter
- (void)setImageNameArray:(NSMutableArray *)imageNameArray {
    if (_imageNameArray != imageNameArray) {
        _imageNameArray = imageNameArray;
        
        // 设置pageControl
        [self.imgPageControl removeFromSuperview];
        self.imgPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 40, self.frame.size.width, 40)];
        [self addSubview:self.imgPageControl];
        // 属性
        self.imgPageControl.currentPage = 0;
        self.imgPageControl.numberOfPages = _imageNameArray.count;
        self.imgPageControl.enabled = NO;
        
        for (int i = 0; i < imageNameArray.count; i++) {
            UIImage *img = [UIImage imageNamed:imageNameArray[i]];
            [self.imageArray addObject:img];
            if (i < 3) {
                [self.imageViewArray[i] setImage:img];
                
            }
        }
        
        // 设置偏移量
        [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        self.index = 0;
        [self layoutImages];
        
        
        /// 初始化timer
        if (self.interval <= 0) {
            self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        } else {
            self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }
        
        
        
#pragma mark - 添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCycleViewAction:)];
        [(UIImageView *)self.imageViewArray[1] addGestureRecognizer:tap];
        
        // 打开交互
        [(UIImageView *)self.imageViewArray[1] setUserInteractionEnabled:YES];
        
    }
}
#pragma mark - 点击手势的响应事件
- (void)tapCycleViewAction:(UITapGestureRecognizer *)tap {
    if (self.tapBlock) {
        self.tapBlock(self.index);
    }
}


#pragma mark - UIScrollViewDelegate
static CGFloat x = 0;
/// 将要开始拖拽时
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    x = scrollView.contentOffset.x;
    
    [self.imgTimer setFireDate:[NSDate distantFuture]];
    
}
/// 减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (x > scrollView.contentOffset.x) {
        [self panToLeft:NO Index:self.index];
    } else if (x < scrollView.contentOffset.x) {
        [self panToLeft:YES Index:self.index];
    }
    
    [self.imgTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
}

#pragma mark - 向左滑动、向右滑动结束切换
- (void)panToLeft:(BOOL)left Index:(NSInteger)index {
    if (!left) {
        self.index = (index - 1 + self.imageNameArray.count) % self.imageNameArray.count;
    } else {
        self.index = (index + 1) % self.imageNameArray.count;
    }
    
    /// 切换图片
    [self layoutImages];
    
    /// 回位操作
    [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
}

/// 切换图片
- (void)layoutImages {
    for (int i = 0; i < 3; i++) {
       [self.imageViewArray[i] setImage:self.imageArray[(self.index - 1 + i + self.imageNameArray.count) % self.imageNameArray.count]];
        
    }
}

#pragma mark - scrollView控制pageControl
/// index的setter方法
- (void)setIndex:(NSInteger)index {
    if (_index != index) {
        _index = index;
        self.imgPageControl.currentPage = index;
    }
    
}



/// 添加block
- (void)addTapBlock:(void (^)(NSInteger))block {
    self.tapBlock = block;
}


@end
